<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div>
    <input type="text" id="username">
    <input type="password" id="password">
    <button id="login">登录</button>
    <button id="register">注册</button>
  </div>
  <script src="./jquery.min.js"></script>
  <script>
    $(function() {
      $('#login').on('click', function() {
        $.ajax({
          url: '/login',
          data: {
            username: $('#username').val(),
            password: $('#password').val()
          },
          dataType: 'json',
          success(res) {
            console.log(res)
            if (res.code === 0) {
              alert('登录成功')
              window.location.href = 'admin.html'
            } else {
              alert(res.msg)
            }
          }
        })
      })
      $('#register').on('click', function() {
        $.ajax({
          url: '/register',
          method: 'post',
          data: {
            username: $('#username').val(),
            password: $('#password').val()
          },
          dataType: 'json',
          success(res) {
            if (res.code === 0) {
              alert('注册成功')
            } else {
              alert(res.msg)
            }
          }
        })
      })
    })
  </script>
</body>
</html>